package com.demo.controller;

import com.demo.common.Result;
import com.demo.service.MybatisTestService;
import com.demo.vo.StudentVO;
import com.demo.vo.TeacherVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: lixiaodong
 * @Description:  mybatis相关
 * @Date: 2022/10/10 13:37
 * @Parms:
 * @ReturnType:
 */
@Controller
@Api(tags = "mybatis相关")
@RequestMapping("/mybatisTest")
public class MybatisTestController {
    @Autowired
    MybatisTestService mybatisTestService;
    @GetMapping("/getTeacherVO")
    @ApiOperation("老师信息(collection测试)")
    @ResponseBody
    public Result getTeacherVO(){
      return mybatisTestService.getTeacherVO();
    }


    @GetMapping("/getStudentVO")
    @ApiOperation("老师信息(collection测试)")
    @ResponseBody
    public Result getStudentVO(){
        return mybatisTestService.getStudentVO();
    }


    @GetMapping("/streamCut")
    @ApiOperation("stream去重")
    @ResponseBody
    public void streamCut(){
        @Data
        class Book{
            public String name;
            public Integer price;
            Book(String name,Integer price){
                this.name = name;
                this.price = price;
            }
        }
        Set<Book> list = new HashSet<>();
        {
            list.add(new Book("Core Java", 200));
            list.add(new Book("Core Java", 200));
            list.add(new Book("Learning Freemarker", 150));
            list.add(new Book("Spring MVC", 200));
            list.add(new Book("Hibernate", 300));
        }
        System.out.println("部分元素去重"+ list);
        System.out.println("原始"+list.size());
//        list.stream().filter(distinctByKey(b -> b.getPrice()))
//                .forEach(b -> System.out.println(b.getName() + "," + b.getPrice()));

//存在重复数据的问题，这里使用stream流的衍生功能，去除一个对象中的部分元素的重复如下：
        ArrayList<Book> collect = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(
                                Book::getPrice))), ArrayList::new));
        System.out.println("部分元素去重"+ collect);
        System.out.println("部分元素去重"+ collect.size());
//多个字段或者多个条件去重
        ArrayList<Book> collect1 = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(p->p.getName() + ";" + p.getPrice()))), ArrayList::new));


        Stream<String> streamBuilder =
                Stream.<String>builder().add("a").add("b").add("c").build();
        streamBuilder.forEach(System.out::println);
    }

}
